<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flattenObject</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const flattenObject = (obj, prefix = '') =>
            Object.keys(obj).reduce((acc, k) => {
                const pre = prefix.length ? `${prefix}.` : '';
                if (
                    typeof obj[k] === 'object' &&
                    obj[k] !== null &&
                    Object.keys(obj[k]).length > 0
                ) {
                    Object.assign(acc, flattenObject(obj[k], pre + k));
                } else {
                    acc[pre + k] = obj[k];
                }
                return acc;
            }, {});
        console.log(flattenObject({ a: { b: { c: 1 } }, d: 1 }));
    </script>
</body>

</html>